//
//  AddImageCell.swift
//  新浪微博项目
//
//  Created by 王炯 on 16/7/29.
//  Copyright © 2016年 王炯. All rights reserved.
//

import UIKit

class AddImageCell: UICollectionViewCell
{
    
    //声明一个闭包，实现关闭按钮的触发事件
    var closeClosure:(() -> ())?
    
    

    //从外界设置的image
    var image:UIImage?{
        
        didSet{
            //添加加号按钮
            if image == nil
            {
                cellImage.image = UIImage(named: "compose_pic_add")
                
                //设置高亮图片
                cellImage.highlightedImage = UIImage(named: "compose_pic_add_highlighted")
                
                //隐藏掉关闭键
                self.closeButton.hidden = true
                
            }
            else
            {
                cellImage.image = image
                
                //设置高亮照片为nil(防止cell复用问题,一定要设置!!!)
                cellImage.highlightedImage = nil
                
                //显示关闭键
                self.closeButton.hidden = false
                
                
            }
        }
        
    }
    
    //在cell上添加的UIImageView
    private lazy var cellImage:UIImageView = UIImageView()
    

    //在cell上添加关闭图片的按钮
    private lazy var closeButton:UIButton = {
       
        let btn = UIButton()
        
        //设置图片
        btn.setImage(UIImage(named:"compose_photo_close"), forState:.Normal)
        
        //设置触发事件
        btn.addTarget(self, action: #selector(AddImageCell.closeClick), forControlEvents: UIControlEvents.TouchUpInside)
        
        
        btn.sizeToFit()
        
        
        return btn
    }()
    
    
    //设置触发按钮的点击事件
    func closeClick()
    {
        closeClosure?()
    }
    
    
    override init(frame: CGRect)
    {
        
        super.init(frame: frame)
        
        //布局界面，在界面上添加image
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder)
    {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //设置界面
    func setUpUI()
    {
        //添加imageView
        addSubview(self.cellImage)
        //添加关闭按钮
        addSubview(self.closeButton)
        
        
        //设置imageView的约束
        self.cellImage.snp_makeConstraints { (make) in
            make.edges.equalTo(UIEdgeInsetsZero)
        }
        
        self.closeButton.snp_makeConstraints { (make) in
            make.trailing.top.equalTo(self)
        }
        
    }
    
    
}
